package com.chaoyong.higo.activity;

import java.util.ArrayList;
import java.util.List;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.cainiaowo.CartProvider;
import com.chaoyong.higo.cainiaowo.ShoppingCart;
import com.chaoyong.higo.fragment.FragNewest;
import com.chaoyong.higo.fragment.FragHomePage;
import com.chaoyong.higo.fragment.FragHomePage.SetClickListener;
import com.chaoyong.higo.fragment.FragWodeduobao;
import com.chaoyong.higo.fragment.FragQingDan;
import com.chaoyong.higo.fragment.FragShaiDan;
import com.chaoyong.higo.utils.DoubleClickExitHelper;
import com.chaoyong.higo.utils.V;
import com.chaoyong.higo.view.BadgeView;
import com.chaoyong.higo.view.MyViewPager;
import com.chaoyong.higo.view.TitleView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.sun.org.apache.bcel.internal.generic.NEW;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends BaseActivity implements OnClickListener, SetClickListener {

	private DoubleClickExitHelper mDoubleClickExit;
	private Context mContext;
	private MyViewPager main_vPager;
	private LinearLayout main_main_lay, main_allgoods_lay, main_newest_lay, main_cart_lay, main_my_lay;
	private ImageView mian_my_img, mian_main_img, mian_allgoods_img, mian_newest_img, mian_cart_img;
	private TextView mian_cart_tx, mian_my_tx, mian_newest_tx, mian_allgoods_tx, mian_main_tx;
	private ArrayList<Fragment> listfreg;
	// 初始化
	private FragHomePage fragHomePage;
	// private FragAllShops fragAllShops;
	private FragNewest fragAllShops;
	// private FragNewsAnnounced fragNewsAnnounced;
	private FragShaiDan fragShaidan;
	// private FragShoppingBar fragShoppingBar;
	private FragQingDan fragQingDan;
	private FragWodeduobao fragMyPurchase;

	private int tag = 10;

	private ProgressDialog pd;

	public static BadgeView badgeView;
	private LinearLayout layout;

	private List<ShoppingCart> carts;
	private CartProvider cartProvider;

	// private int success = 10;

	// @Override
	// protected void onCreate(Bundle arg0) {
	// super.onCreate(arg0);
	// setContentView();
	// findView();
	// init();
	// }

	public void findView() {

		main_vPager = (MyViewPager) findViewById(R.id.main_vPager);

		layout = V.f(this, R.id.main_cart_lay);

		mian_main_img = (ImageView) findViewById(R.id.mian_main_img);
		mian_my_img = (ImageView) findViewById(R.id.mian_my_img);
		mian_cart_img = (ImageView) findViewById(R.id.mian_cart_img);
		mian_allgoods_img = (ImageView) findViewById(R.id.mian_allgoods_img);
		mian_newest_img = (ImageView) findViewById(R.id.mian_newest_img);

		mian_main_tx = (TextView) findViewById(R.id.mian_main_tx);
		mian_my_tx = (TextView) findViewById(R.id.mian_my_tx);
		mian_newest_tx = (TextView) findViewById(R.id.mian_newest_tx);
		mian_allgoods_tx = (TextView) findViewById(R.id.mian_allgoods_tx);
		mian_cart_tx = (TextView) findViewById(R.id.mian_cart_tx);

		main_main_lay = (LinearLayout) findViewById(R.id.main_main_lay);
		main_allgoods_lay = (LinearLayout) findViewById(R.id.main_allgoods_lay);
		main_newest_lay = (LinearLayout) findViewById(R.id.main_newest_lay);
		main_cart_lay = (LinearLayout) findViewById(R.id.main_cart_lay);
		main_my_lay = (LinearLayout) findViewById(R.id.main_my_lay);

		main_main_lay.setOnClickListener(this);
		main_allgoods_lay.setOnClickListener(this);
		main_newest_lay.setOnClickListener(this);
		main_cart_lay.setOnClickListener(this);
		main_my_lay.setOnClickListener(this);

		// PgyUpdateManager.register(this);

	}

	@Override
	public void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		tag = intent.getIntExtra("tag", 11);
		// showToast(tag + "");
		switch (tag) {
		case 0:
			BgChange(0);
			break;
		case 1:
			BgChange(1);
			break;
		case 2:
			BgChange(2);
			break;
		case 3:
			BgChange(3);
			break;
		case 4:
			BgChange(4);
			break;
		default:
			BgChange(0);
			break;
		}
	}

	/*
	 * protected void onStartt() { // TODO Auto-generated method stub
	 * super.onStart(); tag = getIntent().getIntExtra("tag", 10); showToast(tag
	 * + "");
	 * 
	 * switch (tag) { case 0: BgChange(0); break; case 1: BgChange(1); break;
	 * case 2: BgChange(2); break; case 3: BgChange(3); break; case 4:
	 * BgChange(4); break; default: BgChange(0); break; } }
	 */

	public void init() {
		mContext = this;
		cartProvider = new CartProvider(this);

		carts = cartProvider.getAll();

		badgeView = new BadgeView(mContext, layout);
		badgeView.setBadgeMargin(30, 0);
		// badgeView.setBadgeCount(Values.scno);
		if (carts != null) {
			badgeView.setBadgeCount(carts.size());
		} else {
			badgeView.setBadgeCount(0);
		}

		badgeView.show();

		mDoubleClickExit = new DoubleClickExitHelper(this);
		fragHomePage = new FragHomePage();
		fragAllShops = new FragNewest(badgeView);
		fragShaidan = new FragShaiDan();
		// fragShoppingBar = new FragShoppingBar();
		fragQingDan = new FragQingDan(badgeView);
		fragMyPurchase = new FragWodeduobao();

		listfreg = new ArrayList<Fragment>();
		listfreg.add(fragHomePage);
		listfreg.add(fragAllShops);
		listfreg.add(fragShaidan);
		listfreg.add(fragQingDan);
		listfreg.add(fragMyPurchase);

		main_vPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		main_vPager.setOnPageChangeListener(new MyOnPageChangeListener());
		// onStartt();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return mDoubleClickExit.onKeyDown(keyCode, event, this);
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_main_lay:
			BgChange(0);
			break;
		case R.id.main_allgoods_lay:
			BgChange(1);
			break;
		case R.id.main_newest_lay:
			BgChange(2);
			break;
		case R.id.main_cart_lay:
			BgChange(3);
			break;
		case R.id.main_my_lay:
			BgChange(4);
			break;
		}
	}

	class MyPagerAdapter extends FragmentPagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return listfreg.size();
		}

		@Override
		public Fragment getItem(int arg0) {
			return listfreg.get(arg0);
		}

	}

	class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			BgChange(arg0);
		}

	}

	private void BgChange(int tag) {
		if (tag == 0) {
			mian_main_img.setImageResource(R.drawable.duobao);
			mian_main_tx.setTextColor(mContext.getResources().getColor(R.color.red_title));

			mian_allgoods_img.setImageResource(R.drawable.zuixinjiexiao_hui);
			mian_allgoods_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_newest_img.setImageResource(R.drawable.shaidan_hui);
			mian_newest_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_cart_img.setImageResource(R.drawable.qingdan_hui);
			mian_cart_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_my_img.setImageResource(R.drawable.wodeduobao_hui);
			mian_my_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));
			main_vPager.setCurrentItem(0);
		} else if (tag == 1) {
			mian_main_img.setImageResource(R.drawable.duobao_hui);
			mian_main_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_allgoods_img.setImageResource(R.drawable.zuixinjiexiao);
			mian_allgoods_tx.setTextColor(mContext.getResources().getColor(R.color.red_title));

			mian_newest_img.setImageResource(R.drawable.shaidan_hui);
			mian_newest_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_cart_img.setImageResource(R.drawable.qingdan_hui);
			mian_cart_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_my_img.setImageResource(R.drawable.wodeduobao_hui);
			mian_my_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));
			main_vPager.setCurrentItem(1);
		} else if (tag == 2) {
			mian_main_img.setImageResource(R.drawable.duobao_hui);
			mian_main_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_allgoods_img.setImageResource(R.drawable.zuixinjiexiao_hui);
			mian_allgoods_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_newest_img.setImageResource(R.drawable.shaidan);
			mian_newest_tx.setTextColor(mContext.getResources().getColor(R.color.red_title));

			mian_cart_img.setImageResource(R.drawable.qingdan_hui);
			mian_cart_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_my_img.setImageResource(R.drawable.wodeduobao_hui);
			mian_my_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));
			main_vPager.setCurrentItem(2);

		} else if (tag == 3) {
			mian_main_img.setImageResource(R.drawable.duobao_hui);
			mian_main_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_allgoods_img.setImageResource(R.drawable.zuixinjiexiao_hui);
			mian_allgoods_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_newest_img.setImageResource(R.drawable.shaidan_hui);
			mian_newest_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_cart_img.setImageResource(R.drawable.qingdan);
			mian_cart_tx.setTextColor(mContext.getResources().getColor(R.color.red_title));

			mian_my_img.setImageResource(R.drawable.wodeduobao_hui);
			mian_my_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));
			main_vPager.setCurrentItem(3);

		} else if (tag == 4) {
			mian_main_img.setImageResource(R.drawable.duobao_hui);
			mian_main_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_allgoods_img.setImageResource(R.drawable.zuixinjiexiao_hui);
			mian_allgoods_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_newest_img.setImageResource(R.drawable.shaidan_hui);
			mian_newest_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_cart_img.setImageResource(R.drawable.qingdan_hui);
			mian_cart_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_my_img.setImageResource(R.drawable.wodeduobao);
			mian_my_tx.setTextColor(mContext.getResources().getColor(R.color.red_title));
			main_vPager.setCurrentItem(4);
		}
	}

	/**
	 * 更换tab背景，这个实现方法太菜，更好的方法应该使用selecter
	 * 
	 * @param iv
	 *            要换背景的imageview
	 * @param drawable
	 *            图片资源
	 * @param tv
	 *            要换字体颜色的textView
	 * @param color
	 *            颜色资源
	 */
	private void setDiffentBg(ImageView iv, int drawable, TextView tv, int color) {
		iv.setImageResource(drawable);
		tv.setTextColor(mContext.getResources().getColor(color));
	}

	@Override
	public int layoutResID() {
		return R.layout.activity_main;
	}

	@Override
	public void onNewClick() {
		BgChange(2);
	}

	@Override
	public void onShopsClick() {
		BgChange(1);
	}

	@Override
	public void onNIClick() {

	}

	@Override
	public void onUser() {
		BgChange(4);
	}
}
